import random
import speech_recognition as sr

def get_user_choice():
    while True:
        print("请出拳（剪刀/石头/布）：")
        choice = speech_to_text()
        if choice in ['剪刀', '石头', '布']:
            return choice
        else:
            print("输入无效，请重新输入！")

def get_computer_choice():
    choices = ['剪刀', '石头', '布']
    return random.choice(choices)

def determine_winner(user_choice, computer_choice):
    if user_choice == computer_choice:
        return "平局"
    elif (user_choice == '剪刀' and computer_choice == '布') or \
        (user_choice == '石头' and computer_choice == '剪刀') or \
        (user_choice == '布' and computer_choice == '石头'):
        return "用户胜利"
    else:
        return "电脑胜利"

def speech_to_text():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("请开始说话...")
        audio = recognizer.listen(source)
        print("录制到音频：", audio)  # 添加的调试语句
    try:
        choice = recognizer.recognize_google(audio, language='zh-CN')
        print("识别结果：", choice)  # 添加的调试语句
        return choice
    except sr.UnknownValueError:
        print("抱歉，无法识别您的语音！请重试。")
    except sr.RequestError:
        print("抱歉，无法连接到语音识别服务！")

def play_game():
    print("欢迎来到剪刀石头布游戏！")
    while True:
        user_choice = get_user_choice()
        computer_choice = get_computer_choice()
        print("用户选择:", user_choice)
        print("电脑选择:", computer_choice)
        winner = determine_winner(user_choice, computer_choice)
        print("结果:", winner)
        play_again = input("是否再玩一局？(是/否)：")
        if play_again == '否':
            break

play_game()
